//
//  XJCommentViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/6/20.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJCommentViewController.h"
#import "XJCommentHeaderView.h"
#import "XJCommentFooterView.h"

#import "XJAfterSaleModel.h" //头部信息model

@interface XJCommentViewController ()
@property(nonatomic, strong) XJCommentHeaderView *headerView;
@property(nonatomic, strong)  XJCommentFooterView *footerView;
/**标记第一次网络请求返回的数据*/
@property(nonatomic, strong) XJAfterSaleModel *model;
@end

@implementation XJCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"评价";
    
    
    [self requestWhenFirstCome];
    
    
    self.headerView = [[XJCommentHeaderView alloc] initWithFrame:CGRectMake(0, 0, KWidth, 200)];
    [self.view addSubview:self.headerView];
    
    self.footerView = [[XJCommentFooterView alloc] initWithFrame:CGRectMake(0, self.headerView.rb_bottom, KWidth, 300)];
    [self.view addSubview:self.footerView];
    
    XJWeakSelf(self);
    self.footerView.commentButtonBlock = ^{
        [weakself businessLoginOfCommentBtnAction];
    };
}

#pragma mark - 网路请求
/**一进来就要进行的网络请求是*/
- (void)requestWhenFirstCome {
    
    if ([NSString isEmptyString:self.order_goods_id]) {
        XJLog(@"self.order_goods_id为空");
        return;
    }
    
    [XJHttpTool requestOrderOrderPageWithOrderGoodsId:self.order_goods_id successH:^(id responseObjc) {
        
        NSArray *tempArr = responseObjc[@"data"][@"order_goods"];
        NSDictionary *tempDic =  [tempArr firstObject];
        self.model = [XJAfterSaleModel mj_objectWithKeyValues:tempDic];

        dispatch_async(dispatch_get_main_queue(), ^{
            self.headerView.model = self.model;
        });
        
    } failH:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}


#pragma mark - Private method
- (void)businessLoginOfCommentBtnAction {
    
    NSString *content = self.headerView.commentTextView.text;
    if ([NSString isEmptyString:content]) {
        content = @"";
    }
    
    if ([NSString isEmptyString:self.order_goods_id]) {
        XJLog(@"self.order_goods_id:%@ 请求参数为空 操作无效",self.order_goods_id);
        return;
    }
    
    if ([NSString isEmptyString:self.orderSN]) {
        XJLog(@"self.orderSN:%@ 请求参数为空 操作无效",self.orderSN);
        return;
    }
    
    //组装请求参数
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    NSDictionary *commentDic = @{@"option_id":@"0",
                                 @"images":@"",
                                 @"comment_state":self.headerView.commentState,
                                 @"goods_id":self.order_goods_id,
                                 @"content":content,
                                 };
    
    parDic = @{@"comments":@[commentDic],
               @"order_sn":self.orderSN
               }.mutableCopy;
    
    XJLog(@"%@",parDic);
    
    
    [XJHttpTool requestOrderCommentWithDic:parDic successH:^(id responseObjc) {
//        XJLog(@"%@",responseObjc);
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
            [JRToast showWithText:@"感谢您的评论"];
            
            [self.navigationController popViewControllerAnimated:YES];
        }else {
            [JRToast showWithText:responseObjc[@"message"]];
        }
    } failH:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}
//请求参数参考,来源:抓包
/**
 评论请求参数
 http://xinjiangapi.zertone1.com/api/v1/order/comment
 {
 "comments": [{
 "option_id": 0,
 "images": "",
 "comment_state": 1,
 "goods_id": 140,
 "content": "国家科技奖"
 }],
 "order_sn": "SN18061510591700000136"
 }
 
*/

@end
